** This dofile makes the results related to partner change
** Before running this dofile, we have to run prepare_partner_change.do

/*
Table 3: Partner Change during 2004–07, A. Benchmark Regression

Table A1: Weighted Regression: Partner Change during 2004–07

Table A8: Partner Change during 2004–07 (Probit)

Table A12: Partner Change during 2004–07 with Additional Controls: US Upgrading

Table A13: Partner Change during 2004–07 with Additional Controls: Mexico Downgrading

Table A14: Partner Change Regression Controlling for Multi-Product Firms

Table A15: Main Partner Changes Beyond and Within Initial Partners

Table A16: Single Partner Firms and Multiple Partner Firms: US

Table A17: Single Partner Firms and Multiple Partner Firms: Mexico

Table A18: Partner Change during 2004–07 (Bin)

Table A19: Partner Change during 2004–07: Continuous Binding Measure

Table A21: Partner Change during 2004–07: Alternative Binding Measure

Table A23: Gradual Partner Changes

Table A24: Alternative Capability Rankings,  A: Partner Changes during 2004–07: Linear Probability Models

Figure 3: Partner Change during 2004–2007 and Initial Capability Rankings: Apparel Products

Figure A4: Partner Change during 2004–2007 and Initial Capability Ranks: Non-Apparel Products
*/


** For Table 3, A. Benchmark Regression
** US
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 hs6_importer_id using "${output_data}importer_rank.dta"
gen rrank_own=(importer_own_rank-1)/(n_importer_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_down_mv_rank_d0704=1 if e_mv_rank_d0704>0
replace  i_down_mv_rank_d0704=0 if e_mv_rank_d0704<=0
replace  i_down_mv_rank_d0704=. if e_mv_rank_d0704==.
label variable i_e_mv_rank_d0704 "US Partner Upgrading"
label variable i_down_mv_rank_d0704 "US Partner Downgrading"
rename i_e_mv_rank_d0704 UsPartnerUpgrading
rename i_down_mv_rank_d0704 UsPartnerDowngrading

eststo clear
eststo: xi:reg UsPartnerUpgrading Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerUpgrading Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerDowngrading Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerDowngrading  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)

** Mexico
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 hs6_exporter_id using "${output_data}exporter_rank.dta"
gen rrank_own=(exporter_own_rank-1)/(n_exporter_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_up_mv_rank_d0704=1 if mv_rank_d0704<0
replace  i_up_mv_rank_d0704=0 if mv_rank_d0704>=0
replace  i_up_mv_rank_d0704=. if mv_rank_d0704==.
label variable i_up_mv_rank_d0704 "Mex Partner Upgrading"
label variable i_mv_rank_d0704 "Mex Partner Downgrading"

eststo: xi:reg i_up_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo: xi:reg i_up_mv_rank_d0704 Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)

esttab using "${output}partner_change/partner_change_0407.tex", replace label coeflabels(Binding "Binding" rrank_own "OwnRank" rrank_own_Binding "Binding \times OwnRank") nonumbers indicate("Sector FE (HS2)=_Ihs2*")  starlevels(* 0.10 ** 0.05 *** 0.01) keep(Binding rrank_own rrank_own_Binding) cells(b(star fmt(3)) se(par fmt(3))) stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) posthead( &\multicolumn{8}{c}{Liner Probability Models}\\ &\multicolumn{2}{c}{Up^{US}}&\multicolumn{2}{c}{Down^{US}}&\multicolumn{2}{c}{Up^{Mex}}&\multicolumn{2}{c}{Down^{Mex}}\\ &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)\\  \hline) nomtitles



** For Table 3, B. Placebo Check
** US 
** 2007/2008/2009 to 2011
eststo clear
forvalues j=7/9{
use "${output_data}US_for_analysis_0`j'_11_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
eststo: xi:reg i_e_mv_rank_d110`j' Binding i.hs2,  cluster(hs6)
}
** Mexico 
** 2007/2008/2009 to 2011
forvalues j=7/9{
use "${output_data}mexico_for_analysis_0`j'_11_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
eststo: xi:reg i_mv_rank_d110`j' Binding i.hs2 ,  cluster(hs6)
}
esttab using "${output}partner_change/partner_change_placebo.tex", replace nocons nonumbers indicate("Sector FE (HS2)=_Ihs2*") starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3)))  stats(N, fmt(%9.0f) labels(Obs.)) nomtitles varlabels((d)) substitute((d)) posthead( &\multicolumn{6}{c}{Liner Probability Models}\\ &\multicolumn{3}{c}{Up^{US}}&\multicolumn{3}{c}{Down^{Mex}}\\ &2007-11&2008-11&2009-11&2007-11&2008-11&2009-11\\ &(1)&(2)&(3)&(4)&(5)&(6)\\  \hline) nomtitles



** For Table A1
** US
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 hs6_importer_id using "${output_data}importer_rank.dta"
drop _merge
gen rfc=substr(main2004_id, 8, .)
drop if rfc==""
keep if hs6_importer_id!=""
merge m:1 rfc hs6 using "${output_data}sampleweight.dta"
gen weight=1/sampleprob
gen rrank_own=(importer_own_rank-1)/(n_importer_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_down_mv_rank_d0704=1 if e_mv_rank_d0704>0
replace  i_down_mv_rank_d0704=0 if e_mv_rank_d0704<=0
replace  i_down_mv_rank_d0704=. if e_mv_rank_d0704==.
label variable i_e_mv_rank_d0704 "US Partner Upgrading"
label variable i_down_mv_rank_d0704 "US Partner Downgrading"
rename i_e_mv_rank_d0704 UsPartnerUpgrading
rename i_down_mv_rank_d0704 UsPartnerDowngrading

eststo clear
eststo: xi:reg UsPartnerUpgrading  Binding i.hs2 [aw=weight], cluster(hs6)
eststo: xi:reg UsPartnerUpgrading Binding rrank_own rrank_own_Binding i.hs2 [aw=weight], cluster(hs6)
eststo: xi:reg UsPartnerDowngrading  Binding i.hs2 [aw=weight], cluster(hs6)
eststo: xi:reg UsPartnerDowngrading  Binding rrank_own rrank_own_Binding i.hs2 [aw=weight], cluster(hs6)

** Mexico
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 hs6_exporter_id using "${output_data}exporter_rank.dta"
gen rrank_own=(exporter_own_rank-1)/(n_exporter_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen rfc=substr(hs6_exporter_id, 8,.)
drop _merge
drop if rfc==""
merge 1:1 rfc hs6 using "${output_data}sampleweight.dta"
gen weight=1/sampleprob
gen i_up_mv_rank_d0704=1 if mv_rank_d0704<0
replace  i_up_mv_rank_d0704=0 if mv_rank_d0704>=0
replace  i_up_mv_rank_d0704=. if mv_rank_d0704==.
label variable i_up_mv_rank_d0704 "Mex Partner Upgrading"
label variable i_mv_rank_d0704 "Mex Partner Downgrading"

eststo: xi:reg i_up_mv_rank_d0704 Binding i.hs2 [aw=weight], cluster(hs6)
eststo: xi:reg i_up_mv_rank_d0704 Binding rrank_own rrank_own_Binding i.hs2 [aw=weight], cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding i.hs2  [aw=weight] , cluster(hs6)
eststo: xi:reg i_mv_rank_d0704  Binding rrank_own rrank_own_Binding i.hs2  [aw=weight], cluster(hs6)

esttab using "${output}partner_change/partner_change_0407_weighted.tex", replace label coeflabels(Binding "Binding" rrank_own "OwnRank" rrank_own_Binding "Binding \times OwnRank") nonumbers indicate("Sector FE (HS2)=_Ihs2*")  starlevels(* 0.10 ** 0.05 *** 0.01) keep(Binding rrank_own rrank_own_Binding) cells(b(star fmt(3)) se(par fmt(3))) stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) posthead( &\multicolumn{8}{c}{Liner Probability Models}\\ &\multicolumn{2}{c}{Up^{US}}&\multicolumn{2}{c}{Down^{US}}&\multicolumn{2}{c}{Up^{Mex}}&\multicolumn{2}{c}{Down^{Mex}}\\ &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)\\  \hline) nomtitles



** For Table A8
** US
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 hs6_importer_id using "${output_data}importer_rank.dta"
gen rrank_own=(importer_own_rank-1)/(n_importer_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_down_mv_rank_d0704=1 if e_mv_rank_d0704>0
replace  i_down_mv_rank_d0704=0 if e_mv_rank_d0704<=0
replace  i_down_mv_rank_d0704=. if e_mv_rank_d0704==.
label variable i_e_mv_rank_d0704 "US Partner Upgrading"
label variable i_down_mv_rank_d0704 "US Partner Downgrading"

eststo clear
xi:probit i_e_mv_rank_d0704  Binding i.hs2, cluster(hs6)
eststo : margins, dydx(Binding) post
xi:probit i_e_mv_rank_d0704  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo : margins, dydx(Binding rrank_own rrank_own_Binding) post
xi:probit i_down_mv_rank_d0704  Binding i.hs2, cluster(hs6)
eststo : margins, dydx(Binding) post
xi:probit i_down_mv_rank_d0704  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo : margins, dydx(Binding rrank_own rrank_own_Binding) post

** Mexico
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
rename iq_mex_bind80 Binding
merge m:1 hs6_exporter_id using "${output_data}exporter_rank.dta"
gen rrank_own=(exporter_own_rank-1)/(n_exporter_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_up_mv_rank_d0704=1 if mv_rank_d0704<0
replace  i_up_mv_rank_d0704=0 if mv_rank_d0704>=0
replace  i_up_mv_rank_d0704=. if mv_rank_d0704==.
label variable i_up_mv_rank_d0704 "Mex Partner Upgrading"
label variable i_mv_rank_d0704 "Mex Partner Downgrading"

xi:probit i_up_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo : margins, dydx(Binding) vce(unconditional) post
xi:probit i_up_mv_rank_d0704 Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo : margins, dydx(Binding rrank_own rrank_own_Binding) vce(unconditional) post
xi:probit i_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo : margins, dydx(Binding) vce(unconditional) post
xi:probit i_mv_rank_d0704  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo : margins, dydx(Binding rrank_own rrank_own_Binding) vce(unconditional) post

esttab using "${output}partner_change/partner_change_0407_probit.tex", replace label coeflabels(Binding "Binding" rrank_own "OwnRank" rrank_own_Binding "Binding \times OwnRank") nonumbers starlevels(* 0.10 ** 0.05 *** 0.01) keep(Binding rrank_own rrank_own_Binding) cells(b(star fmt(3)) se(par fmt(3))) stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) posthead( &\multicolumn{8}{c}{Probit Models}\\ &\multicolumn{2}{c}{Up^{US}}&\multicolumn{2}{c}{Down^{US}}&\multicolumn{2}{c}{Up^{Mex}}&\multicolumn{2}{c}{Down^{Mex}}\\ &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)\\  \hline) nomtitles



** For Table A12
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
gen rfc=substr(main2004_id, 8, length(main2004_id))
* Merge with LnTotalTrade (Import)
merge m:1 newclid2 hs6 using "${output_data}control_vars/logfirmtrade2004.dta"
drop _merge
* Merge with Maquiladora
merge m:1 newclid2 hs6 using "${output_data}control_vars/mainpartnershare_importer2004.dta"
drop _merge
destring hs6,replace
merge m:1 rfc newclid2 hs6 using "${output_data}control_vars/maquila_ratio_hs6_restrict.dta"
gen maquila_binding=ratio_maquila*Binding
drop _merge
tostring hs6, replace
* Make product type dummies
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
drop _merge
* Merge with #Exporters
merge m:1 hs6 using "${output_data}control_vars/nexp2004.dta"
drop _merge
* Merge with #Importers
merge m:1 hs6 using "${output_data}control_vars/nimp2004.dta"
drop _merge
* Merge with LnTrade
merge m:1 hs6 using "${output_data}control_vars/logtrade2004.dta"
keep if _merge==3
drop _merge
* Merge with #Partners
merge m:1 newclid2 hs6 using "${output_data}control_vars/nseller2004.dta"
keep if _merge==3
drop _merge
* Make US Intermediary
merge m:1 newclid2 using "${output_data}control_vars/Naics.dta" 
gen intermed_only=0
replace intermed_only=1 if ((Retail>0 & Retail!=.) | (Wholesale>0 & Wholesale!=.)) & (Manuf==0 & Manuf!=.)
replace intermed_only=. if Retail==. & Wholesale==.   & Manuf==. 

gen intermed_only_missing=0
replace intermed_only_missing=1 if intermed_only==.
replace intermed_only=0 if intermed_only_missing==1 
label variable i_e_mv_rank_d0704 "US Partner Upgrading"

eststo clear
**firm-product-level
eststo: xi:reg i_e_mv_rank_d0704 Binding limportvalue2004 i.hs2,  cluster(hs6)
eststo: xi:reg i_e_mv_rank_d0704 Binding i.hs2 ratio_maquila,  cluster(hs6)
eststo: xi:reg i_e_mv_rank_d0704  Binding nseller2004 i.hs2, cluster(hs6)
eststo: xi:reg i_e_mv_rank_d0704  Binding intermed_only intermed_only_missing i.hs2, cluster(hs6)
**product-level
eststo: xi:reg i_e_mv_rank_d0704  Binding nexp2004 i.hs2, cluster(hs6)
eststo: xi:reg i_e_mv_rank_d0704  Binding nimp2004 i.hs2, cluster(hs6)
eststo: xi:reg i_e_mv_rank_d0704  Binding lvalue2004 i.hs2, cluster(hs6)
eststo: xi:reg i_e_mv_rank_d0704 Binding woman men cotton Wool manmade i.hs2 , cluster(hs6)
eststo: xi:reg i_e_mv_rank_d0704 Binding limportvalue2004 ratio_maquila nseller2004 intermed_only intermed_only_missing nexp2004 nimp2004 lvalue2004 woman men cotton Wool manmade i.hs2 , cluster(hs6)

esttab using "${output}partner_change/partner_change_0407_us_controls.tex", replace label nonumbers indicate( "Sector FE (HS2)=_Ihs2*" ) starlevels(* 0.10 ** 0.05 *** 0.01) keep(Binding limportvalue2004 ratio_maquila nseller2004 intermed_only nexp2004 nimp2004 lvalue2004 woman men cotton Wool manmade) coeflabels(Binding "Binding" ratio_maquila "Maquiladora" intermed_only "US Intermediary" limportvalue2004 "LnTrade" nseller2004 "N Partners" nexp2004 "N Exporters" nimp2004 "N Importers" lvalue2004 "LnTotalTrade") cells(b(star fmt(3)) se(par fmt(3))) stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) posthead( &\multicolumn{9}{c}{Up^{US}: Linear Probability Models}\\ &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)&(9)\\  \hline) nomtitles



** For Table A13
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
gen rfc=substr(hs6_exporter_id, 8, length(hs6_exporter_id))
destring hs6, replace
* Merge with Maquiladora
merge m:1 rfc newclid2 hs6 using "${output_data}control_vars/maquila_ratio_hs6_restrict.dta"
gen maquila_binding=ratio_maquila*Binding
tostring hs6, replace
drop _merge
* Merge with LnTotalTrade (Export)
merge m:1 rfc hs6 using "${output_data}control_vars/logfirmexport2004.dta"
drop _merge
* Make product type dummies
merge m:1 hs6 using "${rawdata}description_hs6_type.dta"
replace men=0 if hs2!="61"&hs2!="62"
replace woman=0 if hs2!="61"&hs2!="62"
replace cotton=0 if hs2!="61"&hs2!="62"
replace Wool=0 if hs2!="61"&hs2!="62"
replace manmade=0 if hs2!="61"&hs2!="62"
drop _merge
* Merge with #Exporters
merge m:1 hs6 using "${output_data}control_vars/nexp2004.dta"
drop _merge
* Merge with #Importers
merge m:1 hs6 using "${output_data}control_vars/nimp2004.dta"
drop _merge
* Merge with LnTrade
merge m:1 hs6 using "${output_data}control_vars/logtrade2004.dta"
keep if _merge==3
drop _merge
* Merge with #Partners
merge m:1 rfc hs6 using "${output_data}control_vars/nbuyer2004.dta"
keep if _merge==3
drop _merge
* Make US Intermediary
merge m:1 newclid2 using "${output_data}control_vars/Naics.dta" 
gen intermed_only=0
replace intermed_only=1 if ((Retail>0 & Retail!=.) | (Wholesale>0 & Wholesale!=.)) & (Manuf==0 & Manuf!=.)
replace intermed_only=. if Retail==. & Wholesale==.   & Manuf==. 
gen intermed_only_missing=0
replace intermed_only_missing=1 if intermed_only==.
replace intermed_only=0 if intermed_only_missing==1 
label variable i_mv_rank_d0704 "Mex Partner Downgrading"

eststo clear
**firm-product-level
eststo: xi:reg i_mv_rank_d0704 Binding lexportvalue2004 i.hs2  ,  cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding ratio_maquila i.hs2 ,  cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding nbuyer i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding intermed_only intermed_only_missing i.hs2, cluster(hs6)
**product-level
eststo: xi:reg i_mv_rank_d0704 Binding nexp2004 i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding nimp2004 i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding lvalue2004 i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding woman men cotton Wool manmade i.hs2 , cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding lexportvalue2004 ratio_maquila nbuyer intermed_only intermed_only_missing nexp2004 nimp2004 lvalue2004 woman men cotton Wool manmade i.hs2, cluster(hs6)

esttab using "${output}partner_change/partner_change_0407_mex_controls.tex", replace label nonumbers indicate( "Sector FE (HS2)=_Ihs2*" ) starlevels(* 0.10 ** 0.05 *** 0.01) keep(Binding lexportvalue2004 ratio_maquila nbuyer2004 intermed_only nexp2004 nimp2004 lvalue2004 woman men cotton Wool manmade) coeflabels(Binding "Binding" ratio_maquila "Maquiladora" intermed_only "US Intermediary" lexportvalue2004 "LnTrade" nbuyer2004 "N Partners" nexp2004 "N Exporters" nimp2004 "N Importers" lvalue2004 "LnTotalTrade") cells(b(star fmt(3)) se(par fmt(3))) stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) posthead( &\multicolumn{9}{c}{Up^{US}: Linear Probability Models}\\ &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)&(9)\\  \hline) nomtitles



** For Table A14
* US
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 newclid2 using "${output_data}control_vars/importer_nproduct.dta"
gen Binding_nproduct=Binding*nproduct
gen i_down_mv_rank_d0704=1 if e_mv_rank_d0704>0
replace  i_down_mv_rank_d0704=0 if e_mv_rank_d0704<=0
replace  i_down_mv_rank_d0704=. if e_mv_rank_d0704==.
label variable i_e_mv_rank_d0704 "US Partner Upgrading"
label variable i_down_mv_rank_d0704 "US Partner Downgrading"
label variable nproduct "Nproduct"
label variable Binding "Binding"
label variable Binding_nproduct "Binding*Nproduct"

eststo clear
eststo: xi:reg i_e_mv_rank_d0704  Binding nproduct i.hs2, cluster(hs6)
eststo: xi:reg i_e_mv_rank_d0704  Binding nproduct Binding_nproduct i.hs2, cluster(hs6)
eststo: xi:reg i_down_mv_rank_d0704  Binding nproduct i.hs2, cluster(hs6)
eststo: xi:reg i_down_mv_rank_d0704  Binding nproduct Binding_nproduct i.hs2, cluster(hs6)

* Mexico
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
rename iq_mex_bind80 Binding
gen rfc=subinstr(hs6_exporter_id, hs6, "" ,.)
replace rfc=subinstr(rfc, "_", "" ,.)
merge m:1 rfc using "${output_data}control_vars/exporter_nproduct.dta"
gen i_up_mv_rank_d0704=1 if mv_rank_d0704<0
replace  i_up_mv_rank_d0704=0 if mv_rank_d0704>=0
replace  i_up_mv_rank_d0704=. if mv_rank_d0704==.
gen Binding_nproduct=Binding*nproduct
label variable i_up_mv_rank_d0704 "Mex Partner Upgrading"
label variable i_mv_rank_d0704 "Mex Partner Downgrading"
label variable i_up_mv_rank_d0704 "Partner Upgrading"
label variable i_mv_rank_d0704 "Partner Downgrading"
label variable nproduct "Nproduct"
label variable Binding "Binding"
label variable Binding_nproduct "Binding*Nproduct"

eststo: xi:reg i_up_mv_rank_d0704 Binding nproduct i.hs2, cluster(hs6)
eststo: xi:reg i_up_mv_rank_d0704   Binding nproduct Binding_nproduct i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding nproduct i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704   Binding nproduct Binding_nproduct i.hs2, cluster(hs6)

esttab using "${output}partner_change/partner_change_0407_multiproduct.tex", replace label nocons nonumbers indicate( "Hs2-level Effects=_Ihs2*") starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) stats(N, fmt(%9.0f) labels(Obs.)) nomtitles varlabels((d)) collabels(,none) substitute((d)) coeflabels(Binding "Binding" nproduct "N of products" Binding_nproduct "Binding \times N of products") posthead( &\multicolumn{8}{c}{Linear Probability Models}\\ &\multicolumn{2}{c}{Up^{US}}&\multicolumn{2}{c}{Down^{US}}&\multicolumn{2}{c}{Up^{Mex}}&\multicolumn{2}{c}{Down^{Mex}}\\ &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)\\ \hline) nomtitles


** For Table A15
** US 
use "${output_data}us_for_within_across.dta", replace
eststo clear
eststo: xi:reg up_across Binding i.hs2, cluster(hs6)
eststo: xi:reg up_within Binding i.hs2, cluster(hs6)
eststo: xi:reg down_across Binding i.hs2, cluster(hs6)
eststo: xi:reg down_within Binding i.hs2, cluster(hs6)
** Mexico
use "${output_data}mexico_for_within_across.dta", replace
eststo: xi:reg up_across Binding i.hs2, cluster(hs6)
eststo: xi:reg up_within Binding i.hs2, cluster(hs6)
eststo: xi:reg down_across Binding i.hs2, cluster(hs6)
eststo: xi:reg down_within Binding i.hs2, cluster(hs6)

esttab using "${output}partner_change/partner_change_beyond_within.tex", replace nocons nonumbers indicate( "Hs2-level Effects=_Ihs2*")  starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3)))  stats(N, fmt(%9.0f) labels(Obs.)) nomtitles varlabels((d)) collabels(,none) substitute((d)) posthead( &\multicolumn{8}{c}{Liner Probability Models}\\ \hline &\multicolumn{2}{c}{Up^{US}}&\multicolumn{2}{c}{Down^{US}}&\multicolumn{2}{c}{Up^{Mex}}&\multicolumn{2}{c}{Down^{Mex}}\\ \hline &Beyond&Within&Beyond&Within&Beyond&Within&Beyond&Within\\ &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)\\ \hline) nomtitles


** For Table A16
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 hs6_importer_id using "${output_data}importer_rank.dta"
keep if _merge==3
gen rrank_own=(importer_own_rank-1)/(n_importer_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_down_mv_rank_d0704=1 if e_mv_rank_d0704>0
replace  i_down_mv_rank_d0704=0 if e_mv_rank_d0704<=0
replace  i_down_mv_rank_d0704=. if e_mv_rank_d0704==.
gen multi_partner=0
replace multi_partner=1 if n_partner_2004>1
gen multi_partner_Binding=multi_partner*Binding
destring hs2, replace
label variable i_e_mv_rank_d0704 "US Up"
label variable i_down_mv_rank_d0704 "US Down"
rename i_e_mv_rank_d0704 UsPartnerUpgrading
rename i_down_mv_rank_d0704 UsPartnerDowngrading

eststo clear
eststo: reg UsPartnerUpgrading  Binding i.hs2, cluster(hs6)
eststo: reg UsPartnerUpgrading  Binding multi_partner multi_partner_Binding i.hs2, cluster(hs6)
eststo: reg UsPartnerUpgrading  Binding multi_partner multi_partner_Binding i.multi_partner#i.hs2, cluster(hs6)
eststo: reg UsPartnerDowngrading  Binding i.hs2, cluster(hs6)
eststo: reg UsPartnerDowngrading  Binding multi_partner multi_partner_Binding i.hs2, cluster(hs6)
eststo: reg UsPartnerDowngrading  Binding multi_partner multi_partner_Binding i.multi_partner#i.hs2, cluster(hs6)

esttab using  "${output}partner_change/partner_change_multipartner_us.tex", replace label nocons coeflabels(Binding "Binding" multi_partner "Multi-Partner" multi_partner_Binding "Multi-Partner \times Binding") nonumbers indicate("Sector FE (HS2)= **54**")  starlevels(* 0.10 ** 0.05 *** 0.01) keep(Binding multi_partner multi_partner_Binding) cells(b(star fmt(3)) se(par fmt(3)))  stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) posthead( &\multicolumn{6}{c}{Partner Change in Different Periods: Linear Probability Models}\\ \hline &\multicolumn{3}{c}{Up^{US}}&\multicolumn{3}{c}{Down^{US}}\\ \hline &(1)&(2)&(3)&(4)&(5)&(6)\\ \hline) nomtitles


** For Table A17
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 hs6_exporter_id using "${output_data}exporter_rank.dta"
keep if _merge==3
gen rrank_own=(exporter_own_rank-1)/(n_exporter_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_up_mv_rank_d0704=1 if mv_rank_d0704<0
replace  i_up_mv_rank_d0704=0 if mv_rank_d0704>=0
replace  i_up_mv_rank_d0704=. if mv_rank_d0704==.
gen multi_partner=0
replace multi_partner=1 if n_partner_2004>1
gen multi_partner_Binding=multi_partner*Binding
destring hs2, replace
label variable i_up_mv_rank_d0704 "Mex Up"
label variable i_mv_rank_d0704 "Mex Down"

eststo clear
eststo: reg i_up_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo: reg i_up_mv_rank_d0704 Binding multi_partner multi_partner_Binding i.hs2, cluster(hs6)
eststo: reg i_up_mv_rank_d0704 Binding multi_partner multi_partner_Binding i.multi_partner#i.hs2, cluster(hs6)
eststo: reg i_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo: reg i_mv_rank_d0704  Binding multi_partner multi_partner_Binding i.hs2, cluster(hs6)
eststo: reg i_mv_rank_d0704  Binding multi_partner multi_partner_Binding i.multi_partner#i.hs2, cluster(hs6)

esttab using "${output}partner_change/partner_change_multipartner_mex.tex", replace label nocons coeflabels(Binding "Binding" multi_partner "Multi-Partner" multi_partner_Binding "Multi-Partner \times Binding") nonumbers indicate("Sector FE (HS2)= **54**")  starlevels(* 0.10 ** 0.05 *** 0.01) keep(Binding multi_partner multi_partner_Binding) cells(b(star fmt(3)) se(par fmt(3)))  stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) posthead( &\multicolumn{6}{c}{Partner Change in Different Periods: Linear Probability Models}\\ \hline &\multicolumn{3}{c}{Up^{Mex}}&\multicolumn{3}{c}{Down^{Mex}}\\ \hline &(1)&(2)&(3)&(4)&(5)&(6)\\ \hline) nomtitles



** For Table A18
foreach x in 5 10 20{
* US
use "${output_data}US_for_analysis_04_07_hs6_restrict_quantile`x'.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 hs6_importer_id using "${output_data}importer_rank`x'.dta"
gen rrank_own=(importer_own_rank-1)/(`x'-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_down_mv_rank_d0704=1 if e_mv_rank_d0704<0
replace  i_down_mv_rank_d0704=0 if e_mv_rank_d0704>=0
replace  i_down_mv_rank_d0704=. if e_mv_rank_d0704==.
label variable i_e_mv_rank_d0704 "US Partner Upgrading"
label variable i_down_mv_rank_d0704 "US Partner Downgrading"
rename i_e_mv_rank_d0704 UsPartnerUpgrading
rename i_down_mv_rank_d0704 UsPartnerDowngrading

eststo clear
eststo: xi:reg UsPartnerUpgrading Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerUpgrading Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerDowngrading Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerDowngrading Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)

* Mexico
use "${output_data}mexico_for_analysis_04_07_hs6_restrict_quantile`x'.dta", clear
* Set iq_mex_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 hs6_exporter_id using "${output_data}exporter_rank`x'.dta"
gen rrank_own=(exporter_own_rank-1)/(`x'-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_up_mv_rank_d0704=1 if mv_rank_d0704>0
replace  i_up_mv_rank_d0704=0 if mv_rank_d0704<=0
replace  i_up_mv_rank_d0704=. if mv_rank_d0704==.
label variable i_up_mv_rank_d0704 "Mex Partner Upgrading"
label variable i_mv_rank_d0704 "Mex Partner Downgrading"

eststo: xi:reg i_up_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo: xi:reg i_up_mv_rank_d0704 Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)

esttab using "${output}partner_change/partner_change_bin`x'.tex", replace label coeflabels(Binding "Binding" rrank_own "OwnRank" rrank_own_Binding "OwnRank \times Binding") nonumbers indicate( "Sector FE (HS2)=_Ihs2*")  starlevels(* 0.10 ** 0.05 *** 0.01) keep(Binding rrank_own rrank_own_Binding) cells(b(star fmt(3)) se(par fmt(3)))  stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) posthead( &\multicolumn{8}{c}{Linear Probability Models}\\ &\multicolumn{2}{c}{Up^{US}}&\multicolumn{2}{c}{Down^{US}}&\multicolumn{2}{c}{Up^{Mex}}&\multicolumn{2}{c}{Down^{Mex}}\\ &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)\\ \hline) nomtitles
}



** For Table A19
* US
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
gen Binding=bind80_mex
merge m:1 hs6_importer_id using "${output_data}importer_rank.dta"
gen rrank_own=(importer_own_rank-1)/(n_importer_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_down_mv_rank_d0704=1 if e_mv_rank_d0704>0
replace  i_down_mv_rank_d0704=0 if e_mv_rank_d0704<=0
replace  i_down_mv_rank_d0704=. if e_mv_rank_d0704==.
label variable i_e_mv_rank_d0704 "US Partner Upgrading"
label variable i_down_mv_rank_d0704 "US Partner Downgrading"
rename i_e_mv_rank_d0704 UsPartnerUpgrading
rename i_down_mv_rank_d0704 UsPartnerDowngrading

eststo clear
eststo: xi:reg UsPartnerUpgrading  Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerUpgrading  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerDowngrading  Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerDowngrading  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)

* Mexico
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
gen Binding=bind80_mex
merge m:1 hs6_exporter_id using "${output_data}exporter_rank.dta"
gen rrank_own=(exporter_own_rank-1)/(n_exporter_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_up_mv_rank_d0704=1 if mv_rank_d0704<0
replace  i_up_mv_rank_d0704=0 if mv_rank_d0704>=0
replace  i_up_mv_rank_d0704=. if mv_rank_d0704==.
label variable i_up_mv_rank_d0704 "Mex Partner Upgrading"
label variable i_mv_rank_d0704 "Mex Partner Downgrading"

eststo: xi:reg i_up_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo: xi:reg i_up_mv_rank_d0704 Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)

esttab using "${output}partner_change/partner_change_cont_binding_measure.tex", replace label coeflabels(Binding "Binding" rrank_own "OwnRank" rrank_own_Binding "OwnRank \times Binding") nonumbers indicate( "Sector FE (HS2)=_Ihs2*" )  starlevels(* 0.10 ** 0.05 *** 0.01) keep(Binding rrank_own rrank_own_Binding) cells(b(star fmt(3)) se(par fmt(3)) )  stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) posthead( &\multicolumn{8}{c}{Linear Probability Models}\\ &\multicolumn{2}{c}{Up^{US}}&\multicolumn{2}{c}{Down^{US}}&\multicolumn{2}{c}{Up^{Mex}}&\multicolumn{2}{c}{Down^{Mex}}\\ &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)\\ \hline) nomtitles



** For Table A21
* US
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
rename iq_china_bind80 Binding
merge m:1 hs6_importer_id using "${output_data}importer_rank.dta"
gen rrank_own=(importer_own_rank-1)/(n_importer_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_down_mv_rank_d0704=1 if e_mv_rank_d0704>0
replace  i_down_mv_rank_d0704=0 if e_mv_rank_d0704<=0
replace  i_down_mv_rank_d0704=. if e_mv_rank_d0704==.
label variable i_e_mv_rank_d0704 "US Partner Upgrading"
label variable i_down_mv_rank_d0704 "US Partner Downgrading"
rename i_e_mv_rank_d0704 UsPartnerUpgrading
rename i_down_mv_rank_d0704 UsPartnerDowngrading

eststo clear
eststo: xi:reg UsPartnerUpgrading  Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerUpgrading  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerDowngrading  Binding i.hs2, cluster(hs6)
eststo: xi:reg UsPartnerDowngrading  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)

* Mexico
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
rename iq_china_bind80 Binding
merge m:1 hs6_exporter_id using "${output_data}exporter_rank.dta"
gen rrank_own=(exporter_own_rank-1)/(n_exporter_2004-1)
gen rrank_own_Binding=rrank_own*Binding
gen i_up_mv_rank_d0704=1 if mv_rank_d0704<0
replace  i_up_mv_rank_d0704=0 if mv_rank_d0704>=0
replace  i_up_mv_rank_d0704=. if mv_rank_d0704==.
label variable i_up_mv_rank_d0704 "Mex Partner Upgrading"
label variable i_mv_rank_d0704 "Mex Partner Downgrading"

eststo: xi:reg i_up_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo: xi:reg i_up_mv_rank_d0704 Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704 Binding i.hs2, cluster(hs6)
eststo: xi:reg i_mv_rank_d0704  Binding rrank_own rrank_own_Binding i.hs2, cluster(hs6)

esttab using "${output}partner_change/partner_change_alternative_binding_measure.tex", replace label coeflabels(Binding "Binding" rrank_own "OwnRank" rrank_own_Binding "OwnRank \times Binding") nonumbers indicate( "Sector FE (HS2)=_Ihs2*" )  starlevels(* 0.10 ** 0.05 *** 0.01) keep(Binding rrank_own rrank_own_Binding) cells(b(star fmt(3)) se(par fmt(3)) )  stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) posthead( &\multicolumn{8}{c}{Linear Probability Models}\\ &\multicolumn{2}{c}{Up^{US}}&\multicolumn{2}{c}{Down^{US}}&\multicolumn{2}{c}{Up^{Mex}}&\multicolumn{2}{c}{Down^{Mex}}\\ &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)\\ \hline) nomtitles



** For Table A23
eststo clear
* US up 0406
use "${output_data}US_for_analysis_04_06_hs6_restrict.dta", clear
rename iq_mex_bind80 Binding
label variable i_e_mv_rank_d0604 "US Partner Downgrading"
rename i_e_mv_rank_d0604 UsPartnerDowngrading
eststo: xi:reg UsPartnerDowngrading  Binding i.hs2, cluster(hs6)

* US up 0407
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
rename iq_mex_bind80 Binding
label variable i_e_mv_rank_d0704 "US Partner Downgrading"
rename i_e_mv_rank_d0704 UsPartnerDowngrading
eststo: xi:reg UsPartnerDowngrading Binding i.hs2, cluster(hs6)

* US up 0408
use "${output_data}US_for_analysis_04_08_hs6_restrict.dta", clear
rename iq_mex_bind80 Binding
label variable i_e_mv_rank_d0804 "US Partner Downgrading"
rename i_e_mv_rank_d0804 UsPartnerDowngrading
eststo: xi:reg UsPartnerDowngrading Binding i.hs2, cluster(hs6)

* Mexico down 0406
use "${output_data}mexico_for_analysis_04_06_hs6_restrict.dta", clear
rename iq_mex_bind80 Binding
label variable i_mv_rank_d0604 "Mex Partner Downgrading"
rename i_mv_rank_d0604 MexPartnerDowngrading
eststo: xi:reg MexPartnerDowngrading Binding i.hs2, cluster(hs6)

* Mexico down 0407
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
rename iq_mex_bind80 Binding
label variable i_mv_rank_d0704 "Mex Partner Downgrading"
rename i_mv_rank_d0704 MexPartnerDowngrading
eststo: xi:reg MexPartnerDowngrading Binding i.hs2, cluster(hs6)

* Mexico down 0408
use "${output_data}mexico_for_analysis_04_08_hs6_restrict.dta", clear
rename iq_mex_bind80 Binding
label variable i_mv_rank_d0804 "Mex Partner Downgrading"
eststo: xi:reg i_mv_rank_d0804 Binding i.hs2, cluster(hs6)

esttab using "${output}partner_change/gradual_partener_change.tex", replace label coeflabels(Binding "Binding") nonumbers indicate( "Sector FE (HS2)=_Ihs2*" ) starlevels(* 0.10 ** 0.05 *** 0.01) keep(Binding) cells(b(star fmt(3)) se(par fmt(3)))  stats(N, fmt(%9.0f) labels(Obs.)) substitute((d)) posthead( &\multicolumn{6}{c}{Partner Change in Different Periods: Linear Probability Models}\\ \hline &\multicolumn{3}{c}{Up^{US}}&\multicolumn{3}{c}{Down^{Mex}}\\ \hline &2004-06&2004-07&2004-08&2004-06&2004-07&2004-08\\ &(1)&(2)&(3)&(4)&(5)&(6)\\ \hline) nomtitles



** For Table A24, A. Partner Changes during 2004–07: Linear Probability Models
eststo clear
*** US up
* Total
use "${output_data}US_for_analysis_04_07_hs6_restrict_total.dta", clear
rename iq_mex_bind80 Binding
eststo: xi:reg i_e_mv_rank_d0704  Binding i.hs2, cluster(hs6)
* Price
use "${output_data}US_for_analysis_04_07_hs6_price_restrict.dta", clear
rename iq_mex_bind80 Binding
eststo: xi:reg  e_i_p_rank_d0704 Binding i.hs2, cluster(hs6)
* Quality
use "${output_data}US_for_analysis_04_07_hs6_quality_restrict.dta", clear
rename iq_mex_bind80 Binding
eststo: xi:reg e_i_new_rank_d0704  Binding i.hs2, cluster(hs6)

*** US down
* Total
use "${output_data}US_for_analysis_04_07_hs6_restrict_total.dta", clear
rename iq_mex_bind80 Binding
gen i_down_mv_rank_d0704=1 if e_mv_rank_d0704>0
replace  i_down_mv_rank_d0704=0 if e_mv_rank_d0704<=0
replace  i_down_mv_rank_d0704=. if e_mv_rank_d0704==.
eststo: xi:reg i_down_mv_rank_d0704  Binding i.hs2, cluster(hs6)
* Price
use "${output_data}US_for_analysis_04_07_hs6_price_restrict.dta", clear
rename iq_mex_bind80 Binding
gen i_down_p_rank_d0704=1 if e_p_rank_d0704>0
replace  i_down_p_rank_d0704=0 if e_p_rank_d0704<=0
replace  i_down_p_rank_d0704=. if e_p_rank_d0704==.
eststo: xi:reg i_down_p_rank_d0704 Binding i.hs2, cluster(hs6)
* Quality
use "${output_data}US_for_analysis_04_07_hs6_quality_restrict.dta", clear
rename iq_mex_bind80 Binding
gen i_down_new_rank_d0704=1 if e_new_rank_d0704>0
replace  i_down_new_rank_d0704=0 if e_new_rank_d0704<=0
replace  i_down_new_rank_d0704=. if e_new_rank_d0704==.
eststo: xi:reg i_down_new_rank_d0704  Binding i.hs2, cluster(hs6)

*** Mexico up
* Total
use "${output_data}mexico_for_analysis_04_07_hs6_restrict_total.dta", clear
rename iq_mex_bind80 Binding
gen i_up_mv_rank_d0704=1 if mv_rank_d0704<0
replace  i_up_mv_rank_d0704=0 if mv_rank_d0704>=0
replace  i_up_mv_rank_d0704=. if mv_rank_d0704==.
eststo: xi:reg i_up_mv_rank_d0704 Binding i.hs2, cluster(hs6)
* Price
use "${output_data}mexico_for_analysis_04_07_hs6_price_restrict.dta", clear
rename iq_mex_bind80 Binding
gen i_up_p_rank_d0704=1 if p_rank_d0704<0
replace  i_up_p_rank_d0704=0 if p_rank_d0704>=0
replace  i_up_p_rank_d0704=. if p_rank_d0704==.
eststo: xi:reg  i_up_p_rank_d0704 Binding  i.hs2,  cluster(hs6)
* Quality
use "${output_data}mexico_for_analysis_04_07_hs6_quality_restrict.dta", clear
rename iq_mex_bind80 Binding
gen i_up_new_rank_d0704=1 if new_rank_d0704<0
replace  i_up_new_rank_d0704=0 if new_rank_d0704>=0
replace  i_up_new_rank_d0704=. if new_rank_d0704==.
eststo: xi:reg i_up_new_rank_d0704 Binding i.hs2, cluster(hs6)

*** Mexico down
* Total
use "${output_data}mexico_for_analysis_04_07_hs6_restrict_total.dta", clear
rename iq_mex_bind80 Binding
eststo: xi:reg i_mv_rank_d0704 Binding i.hs2, cluster(hs6)
* Price
use "${output_data}mexico_for_analysis_04_07_hs6_price_restrict.dta", clear
rename iq_mex_bind80 Binding
eststo: xi:reg i_p_rank_d0704 Binding i.hs2,  cluster(hs6)
* Quality
use "${output_data}mexico_for_analysis_04_07_hs6_quality_restrict.dta", clear
rename iq_mex_bind80 Binding
eststo: xi:reg i_new_rank_d0704 Binding i.hs2, cluster(hs6)

# delimit ;
esttab using "${output}partner_change/partner_change_alternative_ranking.tex", replace nonumbers indicate("Sector FE (HS2)=_Ihs2*" )
 starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) stats(N, fmt(%9.0f) labels(Obs.))
 posthead( &\multicolumn{12}{c}{A: Partner Changes during 2004–07: Linear Probability Models} &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)&(9)&(10)&(11)&(12)\\ 
 & \multicolumn{3}{c}{USup}&\multicolumn{3}{c}{USdown}& \multicolumn{3}{c}{MEXICOup}&\multicolumn{3}{c}{MEXICOdown}\\
 & Total & Price & Quality & Total & Price & Quality & Total & Price & Quality & Total & Price & Quality \\
 \hline) nomtitles 
 varlabels((d)) collabels(,none) substitute((d));
# delimit cr



****************** FIGURES **********************
** For Figure 3
* US 
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_china_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 hs6 using "${output_data}control_vars/nexp2004.dta"
drop _merge
gen rychange=e_mv_rank_d0704/nexp2004
gen re_mv_rank2004_2=(e_mv_rank2004-1)/(nexp2004-1)
gen re_mv_rank2007_2=(e_mv_rank2007-1)/(nexp2004-1)
gen status=""
replace status="Up" if rychange<0
replace status="Stay" if rychange==0
replace status="Down" if rychange>0
replace status="" if rychange==.
gen up=0 if status!=""
replace up=1 if status=="Up"
gen down=0 if status!=""
replace down=1 if status=="Down"
destring hs2, replace
gen apparel=0
replace apparel=1 if hs2==61 | hs2==62



/** US upgrading for the apparel sector**/
#delimit;
lpoly up re_mv_rank2004_2 if Binding==0 & apparel==1, level(90) msymbol(Oh) pstyle(ci2) fcolor(gs15);
local bw0 = round(r(bwidth),0.01);
lpoly up re_mv_rank2004_2 if  Binding==1 & apparel==1, level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash);
local bw1 = round(r(bwidth),0.01);

twoway  
(lpolyci up re_mv_rank2004_2 if  Binding==0 & apparel==1, level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15)  )
(lpolyci up re_mv_rank2004_2 if  Binding==1 & apparel==1, level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash)),
scheme(s1mono) legend(label(1 "90% CI: Control") label(2 "Control") label(3 "90% CI: Treatment") label(4 "Treatment")) xtitle("Own Relative Rank")
title("Upgrading Probability: US Apparel") note(Bandwidth for the Control: `bw0'. Bandwidth for the Treatment: `bw1');
graph save "Graph" "Up_US_Apparel.gph", replace;
#delimit cr

/** US downgrading for the apparel sector**/
#delimit;
lpoly down re_mv_rank2004_2 if  Binding==0 & apparel==1,level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15);
local bw0 = round(r(bwidth),0.01);
lpoly down re_mv_rank2004_2 if  Binding==1 & apparel==1,level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash);
local bw1 = round(r(bwidth),0.01);

twoway  
(lpolyci down re_mv_rank2004_2 if  Binding==0 & apparel==1, level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15)  )
(lpolyci down re_mv_rank2004_2 if  Binding==1 & apparel==1, level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash)),
scheme(s1mono) legend(label(1 "90% CI: Control") label(2 "Control") label(3 "90% CI: Treatment") label(4 "Treatment")) xtitle("Own Relative Rank")
title("Downgrading Probability: US  Apparel") note(Bandwidth for the Control: `bw0'. Bandwidth for the Treatment: `bw1');
graph save "Graph" "Down_US_Apparel.gph", replace;
#delimit cr

* Mexico
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
merge m:1 hs6 using "${output_data}control_vars/nimp2004.dta"
gen rychange=mv_rank_d0704/nimp2004
gen re_mv_rank2004_2=(mv_rank2004-1)/(nimp2004-1)
gen re_mv_rank2007_2=(mv_rank2007-1)/(nimp2004-1)
* Set iq_china_bind80 as a binding variable
rename iq_mex_bind80 Binding
gen status=""
replace status="Up" if rychange<0
replace status="Stay" if rychange==0
replace status="Down" if rychange>0
replace status="" if rychange==.
gen down=0 if status!=""
replace down=1 if status=="Down"
gen up=0 if status!=""
replace up=1 if status=="Up"
destring hs2, replace
gen apparel=0
replace apparel=1 if hs2==61 | hs2==62

/** Mexico upgrading for the apparel sector**/
#delimit;

lpoly up re_mv_rank2004_2 if  Binding==0 & apparel==1,level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15);
local bw0 = round(r(bwidth),0.01);
lpoly up re_mv_rank2004_2 if  Binding==1 & apparel==1,level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash);
local bw1 = round(r(bwidth),0.01);

twoway  
(lpolyci up re_mv_rank2004_2 if  Binding==0 & apparel==1, level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15))
(lpolyci up re_mv_rank2004_2 if  Binding==1 & apparel==1, level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash)), scheme(s1mono) legend(label(1 "90% CI: Control") label(2 "Control") label(3 "90% CI: Treatment") label(4 "Treatment")) xtitle("Own Relative Rank") title("Upgrading Probability: Mexico Apparel")
title("Upgrading Probability: Mexico Apparel") note(Bandwidth for the Control: `bw0'. Bandwidth for the Treatment: `bw1');
graph save "Graph" "Up_Mex_Apparel.gph", replace;
#delimit cr

/** Mexico downgrading for the apparel sector**/
#delimit;
lpoly down re_mv_rank2004_2 if  Binding==0 & apparel==1,level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15);
local bw0 = round(r(bwidth),0.01);
lpoly down re_mv_rank2004_2 if  Binding==1 & apparel==1,level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash);
local bw1 = round(r(bwidth),0.01);

twoway  
(lpolyci down re_mv_rank2004_2 if  Binding==0 & apparel==1,          level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15)  )
(lpolyci down re_mv_rank2004_2 if  Binding==1 & apparel==1 ,            level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash)),
scheme(s1mono)  legend(off) xtitle("Own Relative Rank")
legend(label(1 "90% CI: Control") label(2 "Control") label(3 "90% CI: Treatment") label(4 "Treatment"))
title("Downgrading Probability: Mexico Apparel") note(Bandwidth for the Control: `bw0'. Bandwidth for the Treatment: `bw1');
graph save "Graph" "Down_Mex_Apparel.gph", replace;
#delimit cr
* output
#delimit;
grc1leg2 "Up_US_Apparel.gph" "Down_US_Apparel.gph" "Up_Mex_Apparel.gph" "Down_Mex_Apparel.gph", xcommon iscale(*.8) scheme(s1mono) legendfrom(Up_US_Apparel.gph) lsize(small);
graph export "${output}partner_change/partner_change_intial_ranking_apparel.png", as(png) replace;
#delimit cr


** For Figure A4
* US
use "${output_data}US_for_analysis_04_07_hs6_restrict.dta", clear
* Set iq_china_bind80 as a binding variable
rename iq_mex_bind80 Binding
merge m:1 hs6 using "${output_data}control_vars/nexp2004.dta"
drop _merge
gen rychange=e_mv_rank_d0704/nexp2004
gen re_mv_rank2004_2=(e_mv_rank2004-1)/(nexp2004-1)
gen re_mv_rank2007_2=(e_mv_rank2007-1)/(nexp2004-1)
gen status=""
replace status="Up" if rychange<0
replace status="Stay" if rychange==0
replace status="Down" if rychange>0
replace status="" if rychange==.
gen up=0 if status!=""
replace up=1 if status=="Up"
gen down=0 if status!=""
replace down=1 if status=="Down"
destring hs2, replace
gen apparel=0
replace apparel=1 if hs2==61 | hs2==62

/** US upgrading for the non-apparel sector**/
#delimit;
lpoly up re_mv_rank2004_2 if  Binding==0 & apparel==0,level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15);
local bw0 = round(r(bwidth),0.01);
lpoly up re_mv_rank2004_2 if  Binding==1 & apparel==0,level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash);
local bw1 = round(r(bwidth),0.01);

twoway  
(lpolyci up re_mv_rank2004_2 if  Binding==0 & apparel==0, level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15))
(lpolyci up re_mv_rank2004_2 if  Binding==1 & apparel==0, level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash)),
scheme(s1mono) legend(label(1 "90% CI: Control") label(2 "Control") label(3 "90% CI: Treatment") label(4 "Treatment")) xtitle("Own Relative Rank")
title("Upgrading Probability: US Non Apparel");
graph save "Graph" "Up_US_NonApparel.gph", replace;
#delimit cr

/** US downgrading for the non-apparel sector**/
#delimit;
lpoly down re_mv_rank2004_2 if  Binding==0 & apparel==0,level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15);
local bw0 = round(r(bwidth),0.01);
lpoly down re_mv_rank2004_2 if  Binding==1 & apparel==0,level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash);
local bw1 = round(r(bwidth),0.01);

twoway  
(lpolyci down re_mv_rank2004_2 if  Binding==0 & apparel==0, level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15))
(lpolyci down re_mv_rank2004_2 if  Binding==1 & apparel==0, level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash)),
scheme(s1mono) legend(label(1 "90% CI: Control") label(2 "Control") label(3 "90% CI: Treatment") label(4 "Treatment")) xtitle("Own Relative Rank")
title("Downgrading Probability: US Non Apparel") note(Bandwidth for the Control: `bw0'.  Bandwidth for the Treatment: `bw1');
graph save "Graph" "Down_US_NonApparel.gph", replace;
#delimit cr

* Mexico
use "${output_data}mexico_for_analysis_04_07_hs6_restrict.dta", clear
merge m:1 hs6 using "${output_data}control_vars/nimp2004.dta"
gen rychange=mv_rank_d0704/nimp2004
gen re_mv_rank2004_2=(mv_rank2004-1)/(nimp2004-1)
gen re_mv_rank2007_2=(mv_rank2007-1)/(nimp2004-1)
rename iq_mex_bind80 Binding
gen status=""
replace status="Up" if rychange<0
replace status="Stay" if rychange==0
replace status="Down" if rychange>0
replace status="" if rychange==.
gen down=0 if status!=""
replace down=1 if status=="Down"
gen up=0 if status!=""
replace up=1 if status=="Up"
destring hs2, replace
gen apparel=0
replace apparel=1 if hs2==61 | hs2==62

/** Mexico upgrading for the non-apparel sector**/
#delimit;
lpoly up re_mv_rank2004_2 if  Binding==0 & apparel==0,level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15);
local bw0 = round(r(bwidth),0.01);
lpoly up re_mv_rank2004_2 if  Binding==1 & apparel==0,level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash);
local bw1 = round(r(bwidth),0.01);

twoway  
(lpolyci up re_mv_rank2004_2 if  Binding==0 & apparel==0, level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15))
(lpolyci up re_mv_rank2004_2 if  Binding==1 & apparel==0, level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash)),
scheme(s1mono) legend(label(1 "90% CI: Control") label(2 "Control") label(3 "90% CI: Treatment") label(4 "Treatment")) xtitle("Own Relative Rank")
title("Upgrading Probability: Mexico Apparel") note(Bandwidth for the Control: `bw0'. Bandwidth for the Treatment: `bw1');
graph save "Graph" "Up_Mex_NonApparel.gph", replace;
#delimit cr

/** Mexico downgrading for the non-apparel sector**/
#delimit;
lpoly down re_mv_rank2004_2 if  Binding==0 & apparel==0,level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15);
local bw0 = round(r(bwidth),0.01);
lpoly down re_mv_rank2004_2 if  Binding==1 & apparel==0,level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash);
local bw1 = round(r(bwidth),0.01);

twoway  
(lpolyci down re_mv_rank2004_2 if  Binding==0 & apparel==0, level(90) msymbol(Oh) pstyle(ci2)  fcolor(gs15))
(lpolyci down re_mv_rank2004_2 if  Binding==1 & apparel==0, level(90) msymbol(s) pstyle(ci2) fcolor(gs10%75) clpattern(dash) alpattern(dash)),
scheme(s1mono) legend(label(1 "90% CI: Control") label(2 "Control") label(3 "90% CI: Treatment") label(4 "Treatment")) xtitle("Own Relative Rank")
title("Downgrading Probability: Mexico Non-Apparel") note(Bandwidth for the Control: `bw0'.  Bandwidth for the Treatment: `bw1');
graph save "Graph" "Down_Mex_NonApparel.gph", replace;
#delimit cr

* output
#delimit;
grc1leg2 "Up_US_NonApparel.gph" "Down_US_NonApparel.gph" "Up_Mex_NonApparel.gph" "Down_Mex_NonApparel.gph", xcommon iscale(*.8) scheme(s1mono) legendfrom(Up_US_NonApparel.gph)  lsize(small);
graph export "${output}partner_change/partner_change_intial_ranking_non_apparel.png", as(png) replace;
#delimit cr


erase "Up_US_NonApparel.gph"
erase "Down_US_NonApparel.gph"
erase "Up_Mex_NonApparel.gph"
erase "Down_Mex_NonApparel.gph"
erase "Up_US_Apparel.gph"
erase "Down_US_Apparel.gph"
erase "Up_Mex_Apparel.gph"
erase "Down_Mex_Apparel.gph"